package com.pms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pms.constant.MessageConstant;
import com.pms.dto.ParkingPageQueryDTO;
import com.pms.dto.ParkingSaveDTO;
import com.pms.dto.ParkingUpdateDTO;
import com.pms.entity.Owner;
import com.pms.entity.Parking;
import com.pms.exception.InsertNotAllowException;
import com.pms.exception.NotFoundException;
import com.pms.mapper.OwnerMapper;
import com.pms.mapper.ParkingMapper;
import com.pms.result.PageResult;
import com.pms.service.ParkingService;
import com.pms.vo.ParkingPageQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.activation.MailcapCommandMap;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class ParkingServiceImpl implements ParkingService {

    @Autowired
    private ParkingMapper parkingMapper;
    @Autowired
    private OwnerMapper ownerMapper;

    /**
     * 新增停车位
     * @param parkingSaveDTO
     */
    public void save(ParkingSaveDTO parkingSaveDTO) {

        Parking parking1=parkingMapper.getByNumber(parkingSaveDTO.getNumber());
        if(parking1 !=null){
            throw new InsertNotAllowException(MessageConstant.RECORD_EXIST);
        }

        Owner owner=ownerMapper.getByOwnerName(parkingSaveDTO.getOwnerName());
        if(owner==null){
            throw new NotFoundException(MessageConstant.OWNER_NOT_EXIST);
        }

        Parking parking = new Parking();
        BeanUtils.copyProperties(parkingSaveDTO,parking);
        parking.setOwnerId(owner.getId());
        parking.setUpdateTime(LocalDateTime.now());

        parkingMapper.save(parking);
    }

    /**
     * 停车位分页查询
     * @param parkingPageQueryDTO
     * @return
     */
    public PageResult page(ParkingPageQueryDTO parkingPageQueryDTO) {
        PageHelper.startPage(parkingPageQueryDTO.getPage(),parkingPageQueryDTO.getPageSize());
        Page<ParkingPageQueryVO> page=parkingMapper.page(parkingPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            parkingMapper.delete(id);
        }
    }

    /**
     * 修改停车位信息
     * @param parkingUpdateDTO
     */
    public void update(ParkingUpdateDTO parkingUpdateDTO) {
        if(Objects.equals(parkingUpdateDTO.getOwnerName(), "")){
            Parking parking=new Parking();
            BeanUtils.copyProperties(parkingUpdateDTO,parking);
            parking.setStatusId(2); // 闲置
            parking.setOwnerId(0); // 无关联业主
            parking.setUpdateTime(LocalDateTime.now());
            parkingMapper.update(parking);
            return;
        }

        Owner owner = ownerMapper.getByOwnerName(parkingUpdateDTO.getOwnerName());
        if(owner==null){
            throw new NotFoundException(MessageConstant.OWNER_NOT_EXIST);
        }

        Parking parking=new Parking();
        BeanUtils.copyProperties(parkingUpdateDTO,parking);
        parking.setStatusId(parkingUpdateDTO.getStatusId());
        parking.setOwnerId(owner.getId());
        parking.setUpdateTime(LocalDateTime.now());
        parkingMapper.update(parking);
    }
}
